package com.pasche;

import java.sql.*;


public class Test {

	public static void main(String[] args) {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			ConexionPool pool = new ConexionPool("jdbc:mysql://localhost:3306/recarga","root","");
			try {
				Connection conn = pool.extraerConexion();
				Statement statement = conn.createStatement();
				String sql_ptos ="SELECT latitud as lat, longitud as lon from puntos";
				
				ResultSet results = statement.executeQuery(sql_ptos);
				double[][] menores = {{1000, 1000, 1000},
									  {1000, 1000, 1000},
									  {1000, 1000, 1000}
				                     }; 
				int pos = 0;
				while(results.next()){
				 
				 double lat = results.getDouble("lat");
				 double lon = results.getDouble("lon");
				 //String sql_distancia="(acos(sin(radians(LATITUD_1)) * sin(radians(LATITUD_2)) + cos(radians(LATITUD_1)) * cos(radians(LATITUD_2)) * cos(radians(LONGITUD_1) - radians(LONGITUD_2))) * 6378) as distancia";
				 int R = 6371;
				 double rLat = Math.toRadians(-32.9550171 - lat);
				 double rLon = Math.toRadians(-60.64647710000001 - lon);
				 double a = Math.sin(rLat/2)*Math.sin(rLat/2) + Math.cos(Math.toRadians(lat)) * Math.cos(Math.toRadians(-32.9550171)) * Math.sin(rLon/2) * Math.sin(rLon/2); 
				 double  c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
				 double dist = R * c;
				 
				 //double dist = Math.sqrt(Math.pow((-32.9550171 - lat), 2)+Math.pow((-60.64647710000001 - lon), 2));
				 
				 if (dist < menores[pos][2]){
					  menores[pos][0]=lat;
					  menores[pos][1]=lon;
					  menores[pos][2]=dist;
				 }
				 
				 for (int j = 0; j < menores.length; j++) {
					 
					if(menores[j][2] > menores[pos][2]){
						pos=j;
					}
					
				 }
				
				}

               for (int j = 0; j < menores.length; j++) {
				System.out.println(menores[j][0] +","+menores[j][1]);
				System.out.println(menores[j][2]);
			   }
					
				
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			
			e.printStackTrace();
		}
		
	}

}
